#include "sphereMesh.h"

using namespace sde;

SphereMesh::SphereMesh(std::string _type, std::string _name, Object* _object) :
    SceneNode(_type,_name,_object),
    entity(0),
    sphereR(1) {
}

void SphereMesh::run() {
    SceneNode::run();

    if (sceneManager && sceneNode) {
        entity = sceneManager->createEntity(object->name,Ogre::SceneManager::PT_SPHERE);
        sceneNode->attachObject(entity);
        sphereR = entity->getBoundingRadius();
        
        updateRadius(object->getVar("radius",float(1)));
        object->subscribeSetVar("radius",boost::bind(&SphereMesh::updateRadius,this,_1));
        
        updateMaterial(object->getVar("material",std::string("Examples/RustySteel")));
        object->subscribeSetVar("material",boost::bind(&SphereMesh::updateMaterial,this,_1));
    }
}

void SphereMesh::updateRadius(Var val) {
    if (boost::any_cast<float>(&val) && entity) {
        float r = boost::any_cast<float>(val);
        float setr = r / sphereR;
        log("set radius (entity radius is %f) %f = %f",sphereR,r,setr);
        object->setVar("scale",Vector3(setr));
    }
}

void SphereMesh::updateMaterial(Var val) {
    if (boost::any_cast<std::string>(&val) && entity) {
        std::string material = boost::any_cast<std::string>(val);
        entity->setMaterialName(material);
    }
}
